/*
 * JSException.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.jslib;

import java.io.PrintStream;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;

/**
 * Thrown when the EcmaScript interpreter detect an error. Package the message
 * of the EcmaScriptException (or other exception) which was generated.
 */
public class JSException extends Exception {

	/** The originating exception. */
	private Throwable originatingException = null;

	/**
	 * Constructs a <code>JSException</code> with the specified detail
	 * message.
	 * 
	 * @param s
	 *            the s
	 */
	public JSException(String s) {
		super(s);
	}

	/**
	 * Constructs a <code>JSException</code> with the specified detail
	 * message, but refereing to the original exception.
	 * 
	 * @param s
	 *            the s
	 * @param originatingException
	 *            the originating exception
	 */
	public JSException(String s, Throwable originatingException) {
		super(s);
		this.originatingException = originatingException;
	}

	/**
	 * Get the originating exception (if any) or null. Look down until a true
	 * originating exception is found, if possible.
	 * 
	 * @return originating exception or null.
	 */
	public Throwable getOriginatingException() {
		Throwable previousLevel = null;
		Throwable currentLevel = originatingException;
		while (currentLevel != null) {
			previousLevel = currentLevel;
			if (currentLevel instanceof JSException) {
				JSException oe = (JSException) originatingException;
				currentLevel = oe.getOriginatingException();
			} else if (currentLevel instanceof EcmaScriptException) {
				EcmaScriptException oe = (EcmaScriptException) originatingException;
				currentLevel = oe.getOriginatingException();
				// } else if (currentLevel instanceof
				// java.lang.reflect.InvocationTargetException) {
				// java.lang.reflect.InvocationTargetException oe =
				// (java.lang.reflect.InvocationTargetException)
				// originatingException;
				// currentLevel = oe.getTargetException();
			} else {
				currentLevel = null;
			}
		} // while

		return previousLevel;
	}

	/**
	 * Prints this <code>Throwable</code> and its backtrace to the standard
	 * error stream.
	 */
	public void printStackTrace() {
		System.err.println(this);
		printStackTrace0(System.err);
	}

	/**
	 * Prints this <code>Throwable</code> and its backtrace to the specified
	 * print stream.
	 * 
	 * @param s
	 *            the s
	 */
	public void printStackTrace(PrintStream s) {
		s.println(this);
		printStackTrace0(s);
	}

	/**
	 * Prints the stack trace0.
	 * 
	 * @param w
	 *            the w
	 */
	private void printStackTrace0(PrintStream w) {
		super.printStackTrace();
		if (originatingException != null) {
			w.println("due to:");
			originatingException.printStackTrace();
		}
		w.flush();
	}
}
